package com.example.combwyaq.visualinterface.activity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;

import com.example.combwyaq.R;
import com.example.combwyaq.entity.ResponseUserEntity;
import com.example.combwyaq.mvp.contract.LoginConteract;
import com.example.combwyaq.mvp.di.DaggerLoginComponent;
import com.example.combwyaq.mvp.di.LoginModels;
import com.example.combwyaq.mvp.model.HttpCode;
import com.example.combwyaq.mvp.model.entity.BaseEntity;
import com.example.combwyaq.mvp.presenter.LoginPresenter;
import com.example.combwyaq.mvp.viwe.BaseActivity;
import com.example.combwyaq.utils.ToastUtils;
import com.example.combwyaq.visualinterface.fragment.IDLogFragment;
import com.example.combwyaq.visualinterface.fragment.PhoneLogFragment;

public class LogActivity extends BaseActivity<LoginPresenter>
        implements LoginConteract.ILoginView, View.OnClickListener {

    private LinearLayout logLl;
    IDLogFragment idLogFragment;
    PhoneLogFragment phoneLogFragment;
    MyBroadcast myBroadcast = new MyBroadcast();
    IntentFilter intentFilter = new IntentFilter();
    private ImageView back;
    private LinearLayout logLinear;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        initView();
    }

    @Override
    protected int addBarColor() {
        return Color.WHITE;
    }

    @Override
    public View addStatusView() {
        return f(R.id.log_linear);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void refreshView(BaseEntity entity) {
        Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
        ResponseUserEntity responseUserEntity= (ResponseUserEntity) entity;
        Intent intent = new Intent();
        intent.setAction("returnUser");
        intent.putExtra("userValues",responseUserEntity.getValues());
        sendBroadcast(intent);
        finish();


    }

    class MyBroadcast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

            if (intent.getAction().equals("200")) {
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.setDuration(1000);
                ObjectAnimator translationY = ObjectAnimator.ofFloat(logLl, "translationY", 0, 200);
                ObjectAnimator alpha = ObjectAnimator.ofFloat(logLl, "alpha", 10, 0);
                animatorSet.playTogether(translationY, alpha);
                animatorSet.start();
                animatorSet.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        AnimatorSet animatorSet = new AnimatorSet();
                        animatorSet.setDuration(1000);
                        ObjectAnimator translationY = ObjectAnimator.ofFloat(logLl, "translationY", 200, 0);
                        ObjectAnimator alpha = ObjectAnimator.ofFloat(logLl, "alpha", 0, 10);
                        animatorSet.playTogether(translationY, alpha);
                        animatorSet.start();

                        fragmentTransaction.show(phoneLogFragment);
                        fragmentTransaction.hide(idLogFragment);
                        fragmentTransaction.commit();

                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {
                    }
                });
            }
            if (intent.getAction().equals("201")) {
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.setDuration(1000);
                ObjectAnimator translationY = ObjectAnimator.ofFloat(logLl, "translationY", 0, 200);
                ObjectAnimator alpha = ObjectAnimator.ofFloat(logLl, "alpha", 10, 0);
                animatorSet.playTogether(translationY, alpha);
                animatorSet.start();
                animatorSet.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        AnimatorSet animatorSet = new AnimatorSet();
                        animatorSet.setDuration(1000);
                        ObjectAnimator translationY = ObjectAnimator.ofFloat(logLl, "translationY", 200, 0);
                        ObjectAnimator alpha = ObjectAnimator.ofFloat(logLl, "alpha", 0, 10);
                        animatorSet.playTogether(translationY, alpha);
                        animatorSet.start();
                        fragmentTransaction.show(idLogFragment);
                        fragmentTransaction.hide(phoneLogFragment);
                        fragmentTransaction.commit();
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {
                    }
                });
            }if (intent.getAction().equals("idlog")){
                String username = intent.getStringExtra("username");
                String password = intent.getStringExtra("password");
                Log.i("www", "onReceive: "+username+password);

                if (username==null){
                    ToastUtils.showMsg("手机号或邮箱不能为空");
                }else{
                    if (password==null){
                        ToastUtils.showMsg("密码不能为空");
                    }else{
                        p.requestLogin(Long.valueOf(username),password, HttpCode.LOGINCODE);
                    }
                }
            }
        }
    }


    @Override
    public int bindLayout() {
        return R.layout.activity_log;
    }
    @Override
    public void initView() {
        logLinear = (LinearLayout) findViewById(R.id.log_linear);
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        phoneLogFragment = new PhoneLogFragment();
        idLogFragment = new IDLogFragment();
        fragmentTransaction.add(R.id.log_ll, phoneLogFragment);
        fragmentTransaction.add(R.id.log_ll, idLogFragment);
        fragmentTransaction.show(phoneLogFragment);
        fragmentTransaction.hide(idLogFragment);
        fragmentTransaction.commit();
        intentFilter.addAction("200");
        intentFilter.addAction("201");
        intentFilter.addAction("idlog");
        registerReceiver(myBroadcast, intentFilter);

        logLl = (LinearLayout) findViewById(R.id.log_ll);
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }


    @Override
    public void inject() {
        DaggerLoginComponent.builder().loginModels(new LoginModels(this))
                .build().injectLogin(this);

    }

    @Override
    public void initData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBroadcast);

    }
}
